package com.luo.shiro;

import org.apache.shiro.config.Ini;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 罗鸿强 on 2018/4/16.
 */
public class ChainDefinitionSectionMetaSource implements FactoryBean<Ini.Section> {
    public static final String PREMISSION_STRING="{0} = authc,perms[{0}] \n";

    public static String DEFAULT_CHAIN="";
    private  String chains="";


    public String getFilterChainDefinitions() {
        return chains;
    }

    public void setFilterChainDefinitions(String filterChainDefinitions) {
        DEFAULT_CHAIN=filterChainDefinitions;
        chains=filterChainDefinitions;


        /**
         * 定义好的菜单权限  添加到需要授权url 中  entity中 权限四张表 Role(角色) UserRole (用户拥有的角色) Power(权限) RolePower(角色拥有的权限) sql
         * */
            String fiter= MessageFormat.format(PREMISSION_STRING,"/vue");
            this.chains =fiter;

            /**
            * 示例代码
            *
            * RoleExample roleExample=new RoleExample();
             RoleExample.Criteria roleCriteria=roleExample.createCriteria();
             roleCriteria.andRoleNameEqualTo("AdminMenu");
             roleCriteria.andStatusEqualTo(1);
             List<Role> roleList= roleMapper.selectByExample(roleExample);
             if(roleList==null || roleList.size()==0){
             this.chains=filterChainDefinitions;
             return;
             }
             Role menuRole=roleList.get(0);

             //获取菜单权限
             RolePowerExample rolePowerExample=new RolePowerExample();
             RolePowerExample.Criteria rolePowerCriteria=rolePowerExample.createCriteria();
             rolePowerCriteria.andRoleIdEqualTo(menuRole.getId());
             List<RolePower> rolePowers=rolePowerMapper.selectByExample(rolePowerExample);
             if(rolePowers==null || rolePowers.size()==0){
             this.chains=filterChainDefinitions;
             return;
             }
             List<Long> ids=new ArrayList<>();
             for(RolePower rolePower:rolePowers){
             ids.add(rolePower.getPowerId());
             }

             if(ids.size()==0){
             this.chains=filterChainDefinitions;
             return;
             }
             PowerExample powerExample=new PowerExample();
             PowerExample.Criteria powerCriteria=powerExample.createCriteria();
             powerCriteria.andStatusEqualTo(1);
             powerCriteria.andIdIn(ids);

             List<Power> powerList=powerMapper.selectByExample(powerExample);
             if(powerList==null || powerList.size()==0){
             this.chains=filterChainDefinitions;
             return ;
             }
             for(Power power:powerList){
             String fiter=MessageFormat.format(PREMISSION_STRING,power.getPower());
             this.chains =fiter;
             }
            * */

        this.chains+=filterChainDefinitions;
    }


    public Ini.Section getObject(){
        Ini ini = new Ini();//网上好多都是在这里配置URL的。但是发现是错误的。
        ini.load(chains);
        Ini.Section section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
        return section;
    }

    public Class<?> getObjectType() {
        return this.getClass();
    }

    public boolean isSingleton() {
        return false;
    }

}
